package com.rich.util;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;

import com.rich.design.R;

import java.io.IOException;

public class TrumpetTestUtils {
	private static Context mContext;

	public TrumpetTestUtils(Context context) {
		mContext = context;
	}

	private MediaPlayer mPlayer;

	private AudioManager mAudioManager;

	public void openTrumpet() {
		try {
			mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
			mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
			mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
					AudioManager.FLAG_PLAY_SOUND);
			mAudioManager.setSpeakerphoneOn(false);
			AssetFileDescriptor fileDescriptor = mContext.getResources().openRawResourceFd(R.raw.tada);
			mPlayer = new MediaPlayer();
			mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
			mPlayer.setLooping(true);
			mPlayer.prepare();
			mPlayer.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void closeTrumpet() {
		mPlayer.stop();
		mAudioManager.setMode(AudioManager.MODE_NORMAL);
	}
}
